<template>
	<view>
		<!-- <z-nav-bar backState="2000" title="打卡"></z-nav-bar> -->
		<!-- 公共组件-每个页面必须引入 -->
		<!--<public-module></public-module>-->
		<clock-card @reloadLocation="reloadLocation" :address="address" :classLists="classLists" />
	</view>
</template>

<script>
import clockCard from './components/card.vue'
import store from '@/store';
export default {
	components:{
		clockCard
	},
	data() {
		return {
			classLists: [],
			punchEndTime: '',
			punchBeginTime: '',
			address: ''
		};
	},
	onLoad(e) {
		
	},
	onShow() {
		console.log('6666666666')
		console.log(uni.getStorageSync('userInfo'), 'uni.getStorageSync()')
		let storeUserInfo = store.state.userInfo;
		if (!storeUserInfo.token) { // nvue页面读取不到vuex里面数据，将取缓存
			storeUserInfo = uni.getStorageInfoSync("userInfo");
		}
		
		let token = storeUserInfo.token;
		if(token) {
			// 校验token是否有效 无效清理本地数据，需重新登录
			this.$api.login.check_wx_token({ token }).then((res) => {
				if(res.code !== 200) {
					//window.sessionStorage.clear();
					uni.clearStorageSync();
					//console.log(token)
					this.judgeLogin(()=>{}, null)
					console.log('登录信息校验失败')
				} else {
					//console.log(token)
					this.judgeLogin(()=>{}, storeUserInfo)
					console.log('登录信息校验成功')
				}
			}).catch(err => {
				//window.sessionStorage.clear();
				uni.clearStorageSync();
				//console.log(token)
				this.judgeLogin(()=>{}, null)
				console.log('登录信息校验失败', err)
			})
		} else {
			this.judgeLogin(()=>{}, null)
		}
			// uni.showModal({
			// 	title: '提示',
			// 	content: `您还没有登陆哦`,
			// 	cancelText: "再逛会",
			// 	confirmText: '去登陆',
			// 	cancelColor: '#81B435',
			// 	success: (res) => {
			// 		if (res.confirm) {
			// 			uni.navigateTo({
			// 			    url: "/subpages/user/loginChange"
			// 			})
			// 		} else if (res.cancel) {
			// 			uni.switchTab({
			// 				url: '/pages/home/home'
			// 			});
			// 		}
			// 	}
			// })
		this.isGetLocation()
		this.getStudentPunchs()
	},
	//方法
	methods: {
		reloadLocation ()  {
			console.log('重新获取位置')
			this.isGetLocation()
		},
		async getStudentPunchs () {
			let userId = this.$store.state.userInfo.userId
			let res = await this.$api.sign.getStudentPunchs(userId)
			this.classLists = res.data
			console.log(res, 'res')
		},
		getAuthorizeInfo(a="scope.userLocation"){  //1. uniapp弹窗弹出获取授权（地理，个人微信信息等授权信息）弹窗
			var _this=this;
			uni.authorize({
				scope: a,
				success() { //1.1 允许授权
					_this.getLocationInfo();
				},
				fail(){    //1.2 拒绝授权
					console.log("你拒绝了授权，无法获得周边信息")
				}
			})
		},
		getLocationInfo(){  //2. 获取地理位置
			var _this=this;
			uni.getLocation({
				type: 'wgs84',
				success (res) {
					console.log("你当前经纬度是：")
					console.log(res)
					let latitude,longitude;
					latitude = res.latitude.toString();
					longitude = res.longitude.toString();
					_this.nowLocation = [latitude, longitude];
					uni.request({
						header:{
							"Content-Type": "application/text"
						},
						url:'http://apis.map.qq.com/ws/geocoder/v1/?location='+latitude+','+longitude+'&key=MVGBZ-R2U3U-W5CVY-2PQID-AT4VZ-PDF35',
						success(re) {
							console.log("中文位置")
							console.log(re)	   
							if(re.statusCode===200){
								console.log("获取中文街道地理位置成功")
								_this.address = re.data.result.formatted_addresses.recommend
							}else{
								console.log("获取信息失败，请重试！")
							}
						 }
					});
				}
			});
		},
		isGetLocation(a="scope.userLocation"){ // 3. 检查当前是否已经授权访问scope属性，参考下截图
			var _this=this;
			uni.getSetting({
				success(res) {					
					if (!res.authSetting[a]) {  //3.1 每次进入程序判断当前是否获得授权，如果没有就去获得授权，如果获得授权，就直接获取当前地理位置
						_this.getAuthorizeInfo()
					}else{
						_this.getLocationInfo()
					}
				}
			});
		},
		change(e) {
			console.log(e)
		}
	},
	//页面下来刷新
	onPullDownRefresh() {},
	//页面上拉触底
	onReachBottom() {},
	//用户点击分享
	onShareAppMessage(e) {
		return this.wxShare();
	}
};
</script>
<style lang="scss" scoped>
@import '@/style/mixin.scss';
</style>
